package com.bida.service.impl;

import com.bida.constant.OrdersConstant;
import com.bida.domain.po.Dish;
import com.bida.domain.po.Orders;
import com.bida.domain.po.Setmeal;
import com.bida.domain.po.User;
import com.bida.domain.vo.BusinessDataVO;
import com.bida.domain.vo.DishOverViewVO;
import com.bida.domain.vo.OrderOverViewVO;
import com.bida.domain.vo.SetmealOverViewVO;
import com.bida.mapper.OrdersMapper;
import com.bida.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import static com.bida.constant.StatusConstant.ENABLE;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private IOrdersService orderService;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private IUserService userService;
    @Autowired
    private IDishService dishService;
    @Autowired
    private ISetmealService setmealService;

    /**
     * 根据时间段统计营业数据
     * @param begin
     * @param end
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        BusinessDataVO businessDataVO = new BusinessDataVO();

        Integer countAll = orderService.lambdaQuery()
                .gt(Orders::getOrderTime, begin)
                .lt(Orders::getOrderTime, end)
                .count();
        List<Orders> ordersList = orderService.lambdaQuery()
                .eq(Orders::getStatus, OrdersConstant.COMPLETED)
                .gt(Orders::getOrderTime, begin)
                .lt(Orders::getOrderTime, end)
                .list();
        // 获取营业额
        BigDecimal turnover = ordersList.stream().map(Orders::getAmount).reduce((x, y) -> x.add(y)).orElse(BigDecimal.valueOf(0.0));
        businessDataVO.setTurnover(turnover.doubleValue());

        // 获取有效订单总数
        Integer validOrderCount = ordersList.size();
        businessDataVO.setValidOrderCount(validOrderCount);

        // 订单完成率
        businessDataVO.setOrderCompletionRate(validOrderCount.doubleValue() / countAll);

        // 平均客单价
        businessDataVO.setUnitPrice(turnover.doubleValue() / validOrderCount);

        // 新增用户数量
        Integer newUsers = userService.lambdaQuery()
                .gt(User::getCreateTime, begin)
                .lt(User::getCreateTime, end)
                        .count();
        businessDataVO.setNewUsers(newUsers);
        return businessDataVO;
    }


    /**
     * 查询订单管理数据
     *
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {
        return ordersMapper.getOrderOverView(LocalDate.now().atStartOfDay());
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        int allCount = dishService.count();

        Integer canBuy = dishService.lambdaQuery()
                .eq(Dish::getStatus, ENABLE)
                .count();

        return DishOverViewVO.builder()
                .sold(canBuy)
                .discontinued(allCount - canBuy)
                .build();
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        int countAllCount = setmealService.count();
        Integer canBuyCount = setmealService.lambdaQuery()
                .eq(Setmeal::getStatus, ENABLE)
                .count();
        return SetmealOverViewVO.builder()
                .sold(canBuyCount)
                .discontinued(countAllCount - canBuyCount)
                .build();
    }
}
